package com.lrl.security.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.lrl.security.dto.User;
import com.lrl.security.dto.UserQueryCondition;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LRL
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

//    /**
//     * 获取用户所有登录信息
//     * @return
//     */
//    @GetMapping("/me")
//    public Object getCurrentUser(){
//        return SecurityContextHolder.getContext().getAuthentication();
//    }

//    /**
//     * 获取用户所有登录信息(简化写法)
//     * @param authentication
//     * @return
//     */
//    @GetMapping("/me")
//    public Object getCurrentUser(Authentication authentication){
//        return authentication;
//    }

    /**
     * 获取所需的登录信息
     * @param userDetails
     * @return
     */
    @GetMapping("/me")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails userDetails){
        return userDetails;
    }

    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult erros){
        if (erros.hasErrors()){
            erros.getAllErrors().stream().forEach(
                    error -> System.out.println(error.getDefaultMessage())
            );
        }
        System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(user.getBirthday());
        user.setId(1);
        return user;
    }

    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult erros){
        if (erros.hasErrors()){
            erros.getAllErrors().stream().forEach(
                    error -> {
                        FieldError fieldError = (FieldError) error;
                        String mseeage = fieldError.getField() +": "+ error.getDefaultMessage();
//                        System.out.println(mseeage);
                        log.error("错误信息：" + mseeage);
                    }
            );
        }
        System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(user.getBirthday());
        user.setId(1);
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id){
        System.out.println(id);
    }

    @GetMapping
    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "用户查询")
    public List<User> query(UserQueryCondition condition, @PageableDefault(page = 2,size = 17,sort = "username,asc") Pageable pageable){
        log.info("UserName ："+condition);
        System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());
        List<User> list = new ArrayList<>();
        list.add(new User());
        list.add(new User());
        list.add(new User());
        return list;
    }

    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    public User getInfo(@ApiParam(value = "用户Id") @PathVariable String id){
//        throw new UserNotExistException(id);
        log.info("进入getInfo服务");
        User user = new User();
        user.setUsername("tom");
        return user;
    }

}
